#coding: utf-8
class CulturalHeritage::CareAndMaintenance < ActiveRecord::Base

  default_scope where("cultural_heritage_care_and_maintenances.deleted = 0")
  belongs_to :obj_care_and_maintenance_type, :class_name => 'CulturalHeritage::CareAndMaintenanceType',  :foreign_key => 'care_and_maintenance_type'
  belongs_to :obj_cultural_institution, :class_name => 'CulturalHeritage::CulturalInstitution',  :foreign_key => 'cultural_institution'
  belongs_to :obj_cultural_heritage, :class_name => 'CulturalHeritage::CulturalHeritage',  :foreign_key => 'cultural_heritage'
  belongs_to :obj_care_and_maintenance_type, :class_name => 'CulturalHeritage::CareAndMaintenanceType', :foreign_key => 'care_and_maintenance_type'

  #cualquier punta responsabvle que no necesariamente es usario del sistema
  belongs_to :obj_security_people, :class_name => 'Security::Person', :foreign_key => 'security_person'
  #miembro de inst. cultural
  belongs_to :obj_care_and_maintenance_inst_cultural, :class_name => 'User', :foreign_key => 'responsible'
  #supervisor
  belongs_to :obj_care_and_maintenance_supervisor, :class_name => 'User', :foreign_key => 'supervisor'
  #expert
  belongs_to :obj_care_and_maintenance_expert, :class_name => 'User', :foreign_key => 'expert'

#  validates :cultural_institution, :presence => {:message => "La Institución Cultural es requerida"}
  validates :cultural_heritage, :presence => {:message => "El Patrimonio Cultural es requerido"}
  validates :care_and_maintenance_type, :presence => {:message => "El Tipo de Cuidado y Mantenimiento es requerido"}
  validates :start_date, :presence => {:message => "La fecha de inicio es requerida"}
  validates :end_date, :presence => {:message => "La fecha final es requerida"}
#  validates :supervisor, :presence => {:message => "supervisor es requerido"}
#  validates :mayor, :presence => {:message => "La magnitud es requerida"}

#  default_scope where("mayor = 0")
#  default_scope where("status = 0")

    def str_care_and_maintenance_type
    if care_and_maintenance_type == 0
      return "Registrado"
    end
    if care_and_maintenance_type == 1
      return "Atendido"
    end
  end

  def str_person_type
    if person_type == 0
      return "Natural"
    end
    if person_type == 1
      return "Jurídico"
    end
  end

  def str_mayor
    if mayor == 0
      return "Menor"
    end
    if mayor == 1
      return "Mayor"
    end
    return "Por definir"
  end

  def str_proceed
    if status == 0
      return "Por Evaluar"
    end
    if status == 1
      return "Procede"
    end
    if status == 2
      return "No Procede"
    end
  end

  def self.count_cultural_heritage_dependencies(id)
      return find(:all,:conditions=>["cultural_heritage_care_and_maintenances.cultural_heritage = ? and (cultural_heritage_care_and_maintenances.deleted = ?) ",id,0],:select=>"count(*) as 'care_and_maintenances'")[0].care_and_maintenances.to_i
  end

  def self.count_cultural_institution_dependencies(id)
      return find(:all,:conditions=>["cultural_heritage_care_and_maintenances.cultural_institution = ? and (cultural_heritage_care_and_maintenances.deleted = ?) ",id,0],:select=>"count(*) as 'care_and_maintenances'")[0].care_and_maintenances.to_i
  end

  def self.count_care_and_maintenance_type_dependencies(id)
      return find(:all,:conditions=>["cultural_heritage_care_and_maintenances.care_and_maintenance_type = ? and (cultural_heritage_care_and_maintenances.deleted = ?) ",id,0],:select=>"count(*) as 'care_and_maintenances'")[0].care_and_maintenances.to_i
  end

   def self.count_person_dependencies(id)
      return find(:all,:conditions=>["(cultural_heritage_care_and_maintenances.security_person = ? or cultural_heritage_care_and_maintenances.supervisor = ? or cultural_heritage_care_and_maintenances.expert = ? ) and (cultural_heritage_care_and_maintenances.deleted = ?) ",id,id,id,0],:select=>"count(*) as 'care_and_maintenances'")[0].care_and_maintenances.to_i
  end
  

end
